<template>
  <div>
    <router-link tag="div" to="/" class="header-abs" v-show="showAbs">
      <span >
          <img class="header-ico-arrow_left header-abs-back" src="@/assets/style/ico/arrow_left.png">
      </span>
      返回
    </router-link>
    <div class="header-fixed" v-show="!showAbs" :style="opacityStyle">
      <span >
          <img class="header-fixed-ico-arrow_left" src="@/assets/style/ico/arrow_left.png">
      </span>
      景点详情
    </div>
  </div>
</template>

<script>
  export default {
    name: 'DetailHeader',
    data() {
      return{
        showAbs: true,
        opacityStyle: {
          opacity: 0
        }
      }
    },
    methods: {
      handleScroll(){

        // const top = document.documentElement.scrollTop;
        //下面的代码解决上面的这行代码，在谷歌浏览器不生效的问题
        const top = document.documentElement.scrollTop + document.body.scrollTop
        if (top > 60){
          let opacity = top / 140;
          opacity = opacity > 1 ? 1 : opacity
          this.opacityStyle = {opacity : opacity}
          this.showAbs = false;
        } else {
          this.showAbs = true;
        }
      }
    },
    mounted() {
      // 使用了不缓存，导致activated不能使用
      window.addEventListener('scroll', this.handleScroll)
    },
    /*activated () {
      //当前页面展示的时候，绑定这个事件
      console.log("1111")
      window.addEventListener('scroll', this.handleScroll)
    },*/
    deactivated () {
      //对全局对象绑定事件的处理，就是解除上面 addEventListener 事件绑定，防止在其他页面也生效
      window.removeEventListener('scroll', this.handleScroll)
    }
  }
</script>

<style scoped lang="stylus">

  @import "~styles/varibles.styl"

  .header-abs
    position: absolute
    left: .2rem
    top: .2rem
    width: .6rem
    height: .6rem
    border-radius: .8rem
    background: rgba(0, 0, 0, .8)
    .header-ico-arrow_left
      width :.64rem;
      height : .64rem;
    .header-abs-back
      color: #fff
      font-size: .4rem
  .header-fixed
    z-index: 2
    position: fixed
    top: 0
    left: 0
    right: 0
    height: $headerHeight
    line-height: $headerHeight
    text-align: center
    color: #fff
    background: $bgColor
    font-size: .32rem
    .header-fixed-ico-arrow_left
      position: absolute
      top: 0
      left: 0
      width: .64rem
      text-align: center
      font-size: .4rem
      color: #fff


</style>
